﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 学生成绩管理系统
{
    public partial class FrmTeacherCx : Form
    {
        public FrmTeacherCx()
        {
            InitializeComponent();
        }
        public FrmTeacherCx(string flag)
        {
            InitializeComponent();
            if (flag == "mod")
            {//修改按钮显示
                this.btnMod.Visible = true;
            }
            else if (flag == "del")
            {//删除按钮显示
                this.BtnDel.Visible = true;
            }
        }
        public void bindDataGird()
        {
            string teaid = this.TxtTeaid.Text.Trim();
            string teaname = this.TxtTeaname.Text.Trim();
            string tearSex = this.comboTearSex.Text;
            TeacherInfoData data = new TeacherInfoData();
            data.Teaid = teaid;
            data.Teaname = teaname;
            data.TearSex = tearSex;
            try
            {
                DataSet ds = TeacherInfoOperation.getTeacherInfo(data);
                this.dataGrid1.DataSource = ds.Tables[0];
            }
            catch(Exception ex)
            {
                ex.ToString();
            }
        }

        private void BtnSearch_Click(object sender, EventArgs e)
        {
            bindDataGird();
        }

        private void btnMod_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择一条要修改的记录
                MessageBox.Show("请选择要修改的记录！", "提示");
                return;
            }
            else
            {
                string teaid = this.TxtTeaid.Text.Trim();
                string teaname = this.TxtTeaname.Text.Trim();
                string tearSex = this.comboTearSex.Text;
                TeacherInfoData data = new TeacherInfoData();
                data.Teaid = teaid;
                data.Teaname = teaname;
                data.TearSex = tearSex;
                DataSet ds = TeacherInfoOperation.getTeacherInfo(data);
                FrmaddTeacher modtea = new FrmaddTeacher(ds.Tables[0].Rows[index]["教师编号"].ToString());
                modtea.ShowDialog(this);
            }
        }

        private void BtnDel_Click(object sender, EventArgs e)
        {
            if (this.dataGrid1.RowCount <= 0)
            {
                MessageBox.Show("请查询要修改的记录！", "提示");
                return;
            }
            int index = this.dataGrid1.CurrentRow.Index;
            if (index < 0)
            {//必须选择一条要删除的记录
                MessageBox.Show("请选择要删除的记录！", "提示");
                return;
            }
            else
            {
                if(MessageBox.Show("确认要删除吗？","删除",MessageBoxButtons.YesNo)==DialogResult.Yes)
                {
                    string teaid = this.TxtTeaid.Text.Trim();
                    string teaname = this.TxtTeaname.Text.Trim();
                    string tearSex = this.comboTearSex.SelectedText;
                    TeacherInfoData data = new TeacherInfoData();
                    data.Teaid = teaid;
                    data.Teaname = teaname;
                    data.TearSex = tearSex;
                    DataSet ds = TeacherInfoOperation.getTeacherInfo(data);
                    try
                    {
                        if (TeacherInfoOperation.deleteTeacherInfo(ds.Tables[0].Rows[index]["教师编号"].ToString()))
                        {
                            MessageBox.Show("删除成功！", "提示");
                            bindDataGird();
                        }
                        else
                        {
                            MessageBox.Show("删除失败！","错误");
                        }
                    }
                    catch(Exception ex)
                    {
                        ex.ToString();
                        MessageBox.Show("删除失败！","错误");
                    }
                }
            }
        }
    }
   // TeacherInfoData
    public class TeacherInfoData
    {
        private string teaid = "";
        private string teaname = "";
        private string tearSex = "";
        private string teaBirthday = "";
        private string teloffice = "";
        private string address = "";
        public string Teaid//教师编号
        {
            get { return teaid; }
            set { teaid = value; }
        }
        public string Teaname//教师姓名
        {
            get { return teaname; }
            set { teaname = value; }
        }
        public string TearSex//教师性别
        {
            get { return tearSex; }
            set { tearSex = value; }
        }
        public string TeaBirthday//教师出生日期
        {
            get { return teaBirthday; }
            set { teaBirthday= value; }
        }
        public string Telooffice//教师办公室电话
        {
            get { return teloffice; }
            set { teloffice = value; }
        }
        public string Address//教师地址
        {
            get{return address;}
            set { address = value; }   
        }
    }
    //TeacherInfoOperation
    public class TeacherInfoOperation
    {
        private static DataAccess dataaccess = new DataAccess();
        public static bool insertTeacherInfo(TeacherInfoData data)
        {//数据插入
            string sql = "insert into 教师信息表(教师编号,教师姓名,教师性别,出生日期,办公室电话,地址)values(" +
                "'" + data.Teaid + "'," +
                "'" + data.Teaname + "'," +
                "'" + data.TearSex + "'," +
                "'" + data.TeaBirthday + "'," +
                "'" + data.Telooffice + "'," +
                "'" + data.Address + "')";
            return dataaccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool updateTeacherInfo(TeacherInfoData data)
        {//数据修改
            string sql = "update 教师信息表 set 教师姓名='" + data.Teaname
            + "'," +
            "教师性别='" + data.TearSex + "'," +
            "出生日期='" + data.TeaBirthday + "'," +
            "办公室电话='" + data.Telooffice + "'," +
            "地址='" + data.Address + "'" +
            "where 教师编号='" + data.Teaid + "'";
            return dataaccess.ExecuteSQL(sql);//执行sql语句
        }
        public static bool deleteTeacherInfo(string teaid)
        {//数据删除
            string sql = String.Format("delete 教师信息表 where 教师编号='{0}'",teaid);
            return dataaccess.ExecuteSQL(sql);//执行sql语句
        }
        public static DataSet getTeacherInfo(TeacherInfoData data)
        {//数据查询
            string condition = "";//查询条件
            if(data.Teaid!=null&&""!=data.Teaid)
            {
                condition += "and 教师编号='" + data.Teaid + "'";
            }
            if(data.Teaname!=null&&""!= data.Teaname)
            {
                condition += " and 教师姓名='" + data.Teaname + "'";
            }
            if(data.TearSex!=null&&""!=data.TearSex)
            {
                condition += " and 教师性别='" + data.TearSex + "'";
            }
            string sql = "select 教师编号,教师姓名,教师性别,出生日期,办公室电话,地址 from 教师信息表 where 1=1" + condition;
            return dataaccess.GetDataSet(sql, "教师信息表");//执行sql语句
        }
    }
}